package me.minecraft.plugin.playerride.command;

import me.minecraft.plugin.playerride.manager.MainManager;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
public class ProtectCommand extends SubCommand {

    @Override
    public @NotNull String getName() {
        return "protect";
    }

    @Override
    public @Nullable String getPermission() {
        return "PlayerRide.command.protect";
    }

    @Override
    public void onCommand(Player player, String[] args) {
        Player target = player;
        if (args.length == 1) {
            if (!player.hasPermission("PlayerRide.command.protect.other")) {
                player.sendMessage("[§c玩家骑乘§f] 你没有权限设置他人保护模式");
                return;
            }
            target = player.getServer().getPlayer(args[0]);
            if (target == null) {
                player.sendMessage("[§c玩家骑乘§f] 玩家不存在");
                return;
            }
        }
        if (MainManager.INSTANCE.isProtected(target)) {
            MainManager.INSTANCE.disableProtection(target);
            if (player == target) {
                player.sendMessage("[§a玩家骑乘§f] 已关闭保护模式，其他玩家可以骑乘你");
            } else {
                player.sendMessage("[§a玩家骑乘§f] 已关闭 " + target.getName() + " 保护模式");
                target.sendMessage("[§a玩家骑乘§f] 你的保护模式被 " + player.getName() + " 关闭");
            }
        } else {
            MainManager.INSTANCE.enableProtection(target);
            List<Entity> passengers = target.getPassengers();
            if (player == target) {
                player.sendMessage("[§a玩家骑乘§f] 已开启保护模式，其他玩家无法骑乘你");
            } else {
                player.sendMessage("[§a玩家骑乘§f] 已开启 " + target.getName() + " 保护模式");
                target.sendMessage("[§a玩家骑乘§f] 你的保护模式被 " + player.getName() + " 开启");
            }
            for (Entity passenger : passengers) {
                if (passenger.getType() != EntityType.PLAYER) continue;
                target.removePassenger(passenger);
            }
        }
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, String[] args) {
        return null;
    }

}
